﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

namespace Kortex.KPDabaBase
{
    class Kpdabase
    {
        private static Kpdabase instance = null;
        string localDir = string.Empty;
        Dictionary<string, string> WallMgrTable = new Dictionary<string, string>();
        string WallMgrTable_file = string.Empty;

        private Kpdabase(string LocalPath)
        {
            localDir = LocalPath;
            Init_wallMgr_Table();
        }


        public static Kpdabase GetInstance(string Path)
        {
            if (instance == null)
            {
                instance = new Kpdabase(Path);
            }

            return instance;
        }

        private void Init_wallMgr_Table()
        {
            WallMgrTable_file = localDir + "//MerucryDataBse.db"; //Strange: i have keep this name as this table is allready Shiped at River side univercity California USA
            FileInfo MyFile = new FileInfo(WallMgrTable_file);
            if (!MyFile.Exists) return;

            FileStream FStream = null;
            FStream = File.OpenRead(WallMgrTable_file);
            int count = 0;
            byte[] Buff = new Byte[FStream.Length];
            count = FStream.Read(Buff, 0, (Int32)FStream.Length);
            FStream.Close();
            string WhoteDB = System.Text.ASCIIEncoding.ASCII.GetString(Buff);
            string[] DBparts = WhoteDB.Split("\n".ToCharArray());
            string[] dbValue;
            //GenralDB
            foreach (string str in DBparts)
            {
                try
                {
                    dbValue = str.Split('=');
                    WallMgrTable[dbValue[0]] = dbValue[1].Trim();
                }
                catch (Exception e)
                { }
            }

        }

        public void Commit_wallMgr_Table()
        {
            SaveWallMgr_table(WallMgrTable_file);

        }

        #region string to Byte Array
        private static byte C2b(char c)
        {
            if ((int)c < 256) return (byte)c;
            throw new Exception("character overflows a byte");
        }
        private static byte[] S2b(string value)
        {
            byte[] buffer = new byte[value.Length];
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = C2b(value[i]);
            }

            return buffer;
        }
        #endregion



        private void SaveWallMgr_table(string fileName)
        {
            
            FileStream FStream = null;
            FStream = File.OpenWrite(fileName);
            FStream.Seek(0, SeekOrigin.Begin);
            string dab;
            byte[] NotapasNewline = new byte[2];

            NotapasNewline[0] = (byte)'\r';
            NotapasNewline[1] = (byte)'\n';

            foreach (KeyValuePair<string, string> item in WallMgrTable)
            {
                dab = item.Key + "=" + item.Value;
                FStream.Write(S2b(dab), 0, dab.Length);
                FStream.Write(NotapasNewline, 0, 2);
            }

            FStream.Close();


        }

        public string GetWallMgr_table_val(string key)
        {
            string val = string.Empty;
         
            Monitor.Enter(WallMgrTable);
            try
            {
                val = WallMgrTable[key];
            }
            catch (Exception e)
            {
                //val = string.Empty;
            }

            Monitor.Exit(WallMgrTable);

         return val;
        }


        public void Set_WallMgr_table_val(string key,string Value)
        {
            Monitor.Enter(WallMgrTable);
                WallMgrTable[key] = Value;
            Monitor.Exit(WallMgrTable);
        }


    }
}
